<?php

declare(strict_types=1);

/*
 * This file is part of Sulu.
 *
 * (c) Sulu GmbH
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Sulu\Component\Webspace\Tests\Unit\Manager;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Component\Webspace\Manager\Dumper\PhpWebspaceCollectionDumper;
use Sulu\Component\Webspace\Manager\WebspaceCollection;

class PhpWebspaceCollectionDumperTest extends TestCase
{
    use ProphecyTrait;

    /**
     * @var ObjectProphecy<WebspaceCollection>
     */
    private $webspaceCollection;

    /**
     * @var PhpWebspaceCollectionDumper
     */
    private $webspaceDumper;

    public function setUp(): void
    {
        $this->webspaceCollection = $this->prophesize(WebspaceCollection::class);
        $this->webspaceDumper = new PhpWebspaceCollectionDumper($this->webspaceCollection->reveal());
    }

    public function testGenerate(): void
    {
        $this->webspaceCollection->toArray()->shouldBeCalled()->willReturn([]);

        $string = $this->webspaceDumper->dump(['cache_class' => 'CacheClass', 'base_class' => 'BaseClass']);

        $expected = '<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * CacheClass
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class CacheClass extends BaseClass
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();



        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}


';
        $this->assertEquals($expected, $string);
    }

    public function testGenerateWithData(): void
    {
        $this->webspaceCollection
               ->toArray()
               ->shouldBeCalled()
               ->willReturn(['webspaces' => [['key' => 'some_webspace', 'name' => 'Somewebspace']]])
        ;
        $string = $this->webspaceDumper->dump(['cache_class' => 'CacheClass', 'base_class' => 'BaseClass']);

        $expected = '<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * CacheClass
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class CacheClass extends BaseClass
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();

        // new webspace
        $webspace = new Webspace();
        $webspace->setKey(\'some_webspace\');
        $webspace->setName("Somewebspace");
        // add theme



        // add navigation
        $navigation = new Navigation();
        $webspace->setNavigation($navigation);
        $webspace->setResourceLocatorStrategy(\'\');

        $webspaceRefs[\'some_webspace\'] = $webspace;




        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}


';

        $this->assertSame($expected, $string);
    }

    public function testGenerateWithSpecialData(): void
    {
        $this->webspaceCollection
               ->toArray()
               ->shouldBeCalled()
               ->willReturn(['webspaces' => [['key' => 'some_webspace', 'name' => 'Joe\'s webspace']]])
        ;
        $string = $this->webspaceDumper->dump(['cache_class' => 'CacheClass', 'base_class' => 'BaseClass']);

        $expected = '<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * CacheClass
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class CacheClass extends BaseClass
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();

        // new webspace
        $webspace = new Webspace();
        $webspace->setKey(\'some_webspace\');
        $webspace->setName("Joe\'s webspace");
        // add theme



        // add navigation
        $navigation = new Navigation();
        $webspace->setNavigation($navigation);
        $webspace->setResourceLocatorStrategy(\'\');

        $webspaceRefs[\'some_webspace\'] = $webspace;




        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}


';

        $this->assertSame($expected, $string);
    }

    public function testGenerateWithAccentedData(): void
    {
        $this->webspaceCollection
               ->toArray()
               ->shouldBeCalled()
               ->willReturn(['webspaces' => [['key' => 'some_webspace', 'name' => 'Àccéntèd webspacê']]])
        ;
        $string = $this->webspaceDumper->dump(['cache_class' => 'CacheClass', 'base_class' => 'BaseClass']);

        $expected = '<?php
use Sulu\Component\Localization\Localization;
use Sulu\Component\Webspace\Manager\WebspaceCollection;
use Sulu\Component\Webspace\Portal;
use Sulu\Component\Webspace\PortalInformation;
use Sulu\Component\Webspace\Environment;
use Sulu\Component\Webspace\Security;
use Sulu\Component\Webspace\Segment;
use Sulu\Component\Webspace\Url;
use Sulu\Component\Webspace\CustomUrl;
use Sulu\Component\Webspace\Webspace;
use Sulu\Component\Webspace\Navigation;
use Sulu\Component\Webspace\NavigationContext;

/**
 * CacheClass
 *
 * DO NOT EDIT
 * This file is autogenerated by the Sulu Webspace component
 */
class CacheClass extends BaseClass
{
    public function __construct()
    {
        $webspaceRefs = array();
        $portalRefs = array();
        $localizationRefs = array();
        $segmentRefs = array();
        $portalInformationRefs = array();

        // new webspace
        $webspace = new Webspace();
        $webspace->setKey(\'some_webspace\');
        $webspace->setName("Àccéntèd webspacê");
        // add theme



        // add navigation
        $navigation = new Navigation();
        $webspace->setNavigation($navigation);
        $webspace->setResourceLocatorStrategy(\'\');

        $webspaceRefs[\'some_webspace\'] = $webspace;




        $this->setWebspaces($webspaceRefs);
        $this->setPortals($portalRefs);
        $this->setPortalInformations($portalInformationRefs);
    }
}


';

        $this->assertSame($expected, $string);
    }
}
